"use strict";

import Ajax from "../libs/ajax";
import Constants from "../libs/constants"

//获取下载路径信息
export function getDownloadUrl(rec_id, type) {
    return Ajax.ajax({
        url: Constants.URL_GETDOWNLOAD_URL + `?rec_id=${rec_id}&type=${type}`
    }).then(res => res.data)
}

export function getHistoryData(page_num, page_size, type, user_name, user_type, date_from, date_to,group_name,campus_name,class_name) {
    const baseUrl = "/api/record/query?";
    if (page_num === undefined || page_size === undefined || type === undefined) {
        return new Promise((resolve, reject) => {
            resolve({ code: 2011, msg: "员工查询参数缺失" });
        });
    }
    /*to-do:参数校验
    //除了required的键，其余参数容许缺失。
    //required:page_num存在->为数字->上下限
    if(page_num===undefined){
      console.log("页面缺失,默认值：1");
    }
    //required:page_size存在->为数字->上下限
    if(page_size===undefined){
      console.log("页面缺失，默认值：3");
    }
    //user_name存在->长度限制->恶意编码
    if(user_name===undefined){
      console.log("返回全页");
    }
    */
    let queryArg = [
        `page_num=${page_num}`,
        `page_size=${page_size}`,
        `type=${type}`
    ];

    if (judgeParam(user_name)) {
        queryArg.push(`user_name=${user_name}`);
    }

    if (judgeParam(user_type)) {
        queryArg.push(`user_type=${user_type}`);
    }

    if (judgeParam(date_from) && judgeParam(date_to)) {
        queryArg.push(`date_from=${date_from}`);
        queryArg.push(`date_to=${date_to}`);
    }
    if (judgeParam(group_name)) {
        queryArg.push(`group_name=${group_name}`);
    }
    if (judgeParam(campus_name)) {
        queryArg.push(`campus_name=${campus_name}`);
    }
    if (judgeParam(class_name)) {
        queryArg.push(`class_name=${class_name}`);
    }
    return Ajax.ajax({
        url: baseUrl + queryArg.join("&"),
        timeout: 100 * 1000,
        method: "get"
    }).then(res => res.data);
}

function judgeParam(param) {
    return param !== undefined && param !== ""
}

export function getAvatar(rec_id, user_id) {
    return Ajax.ajax({
        url: `/api/record/query_avatar?user_id=${user_id}&rec_id=${rec_id}`,
        method: "get"
    }).then(res => res.data);
}

export function getStangerAvatar(rec_id) {
    return Ajax.ajax({
        url: `/api/record/query_avatar?rec_id=${rec_id}`,
        method: "get"
    }).then(res => res.data);
}

export function getTypeList(type) {
    return Ajax.ajax({
        url: `/api/config/query?type=${type}`,
        method: "get"
    }).then(res => res.data);
}

export function stat() {
    return Ajax.ajax({
        url: `/api/stat/counting`,
        timeout: 100 * 1000,
        method: "get"
    }).then(res => res.data);
}


export async function read(uri) {
    function getBase64Image(img) {
        let canvas = document.createElement("canvas");
        canvas.width = img.width;
        canvas.height = img.height;

        var ctx = canvas.getContext("2d");
        ctx.drawImage(img, 0, 0, img.width, img.height);
        var ext = img.src.substring(img.src.lastIndexOf(".") + 1).toLowerCase();
        var dataURL = canvas.toDataURL("image/" + ext);
        return dataURL;
    }

    return new Promise((resolve, reject) => {
        let image = new Image();
        image.crossOrigin = '';
        image.src = uri;
        image.onload = function () {
            var base64 = getBase64Image(image);
            resolve(base64);
        }
    });
}

export function cleanRecord() {
    return Ajax.ajax({
        url: Constants.URL_RECORD_CLEAN,
        method: 'post'
    }).then(res => res.data);
}

export function recordToUser(data) {
    return Ajax.ajax({
        url: Constants.URL_RECORD_TO_USER,
        method: 'post',
        data: data
    }).then(res => res.data);
}

export function getStudentCapturePhoto(data) {
    return Ajax.ajax({
        url: '/api/record/get_student_capture_photo',
        method: 'post',
        timeout: 1 * 60 * 1000,
        data
    }).then(res => res.data)
}

export function getStudentRecord(data) {
    return Ajax.ajax({
        url: '/api/record/get_student_record',
        method: 'post',
        timeout: 1 * 60 * 1000,
        data
    }).then(res => res.data)
}